package com.bilibili.liveremind.module;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ReUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 直播间信息
 */
public class LiveInfo {

    public static void main(String[] args) {
        //获取主播主页信息
        //https://api.bilibili.com/x/space/wbi/acc/info?mid=6326729&token=&platform=web&web_location=1550101&w_rid=8e7581fd76e061a7f7031d30c64192cb&wts=1708408615
        //获取直播间开播信息
        //未开播：https://live.bilibili.com/544837?broadcast_type=0&is_room_feed=1&spm_id_from=333.999.to_liveroom.0.click&live_from=86002
        //已开播：https://live.bilibili.com/6326729?broadcast_type=0&is_room_feed=1&spm_id_from=333.999.to_liveroom.0.click&live_from=86002
        //https://live.bilibili.com/房间号?
        //固定参数：
        //broadcast_type=0 广播类型
        //is_room_feed=1 房间提供 猜想 1可以直播 0不可以直播
        //spm_id_from=333.999.to_liveroom.0.click 不清楚
        //live_from=86002 不清楚
        //获取用户名和房间号
        //获取标题
        //获取开播时间、https://api.bilibili.com/x/space/acc/info
        //https://api.live.bilibili.com/xlive/web-room/v2/index/getRoomPlayInfo?room_id=6326729
        /**
         * {
         *     "code": 0,
         *     "message": "0",
         *     "ttl": 1,
         *     "data": {
         *         "room_id": 6326729,
         *         "short_id": 0,
         *         "uid": 21500070,
         *         "is_hidden": false,
         *         "is_locked": false,
         *         "is_portrait": false,
         *         "live_status": 0, 直播状态
         *         "hidden_till": 0,
         *         "lock_till": 0,
         *         "encrypted": false,
         *         "pwd_verified": true,
         *         "live_time": 0,
         *         "room_shield": 0,
         *         "all_special_types": [],
         *         "playurl_info": null,
         *         "official_type": 0,
         *         "official_room_id": 0,
         *         "risk_with_delay": 0
         *     }
         * }
         */
        //https://api.live.bilibili.com/xlive/web-room/v2/index/getRoomPlayInfo?room_id=6326729&protocol=0,1&format=0,1,2&codec=0,1,2&qn=0&platform=web&ptype=8&dolby=5&panorama=1
        //https://api.live.bilibili.com/xlive/web-room/v2/index/getRoomPlayInfo?room_id=6326729&qn=0&platform=web&ptype=8&dolby=5&panorama=1
        //https://api.live.bilibili.com/xlive/web-room/v2/index/getRoomPlayInfo?room_id=22495291
        /**
         * {
         *           "code":  1002002,
         *           "message":  "参数错误",
         *           "ttl":  1,
         *           "data":  {
         *                     "room_id":  22495291,
         *                     "short_id":  0,
         *                     "uid":  674539396,
         *                     "is_hidden":  false,
         *                     "is_locked":  false,
         *                     "is_portrait":  false,
         *                     "live_status":  1,
         *                     "hidden_till":  0,
         *                     "lock_till":  0,
         *                     "encrypted":  false,
         *                     "pwd_verified":  true,
         *                     "live_time":  1708656500,
         *                     "room_shield":  1,
         *                     "all_special_types":  [
         *                               50
         *                     ],
         *                     "playurl_info":  null,
         *                     "official_type":  0,
         *                     "official_room_id":  0,
         *                     "risk_with_delay":  0
         *           }
         * }
         */

        //room_id 房间号
        //房间号
        String roomId = "2322181"; //Nuoiz
        //获取直播状态
        //https://api.live.bilibili.com/xlive/web   -room/v2/index/getRoomPlayInfo?room_id=2322181&protocol=0,1&format=0,1,2&codec=0,1,2&qn=0&platform=web
        String biliRoomStatusUrl = "https://api.live.bilibili.com/xlive/web-room/v2/index/getRoomPlayInfo";
        //参数
        Map<String, Object> biliRoomStatusUrlParam = new HashMap<>();
        biliRoomStatusUrlParam.put("room_id",roomId); //房间号
        //类似签名的参数
        biliRoomStatusUrlParam.put("protocol","0,1");
        biliRoomStatusUrlParam.put("format","0,1,2");
        biliRoomStatusUrlParam.put("codec","0,1,2");
        biliRoomStatusUrlParam.put("qn","0");
        biliRoomStatusUrlParam.put("platform","web");
        //使用hutool发送请求 get请求
        String liveResult = HttpUtil.get(biliRoomStatusUrl,biliRoomStatusUrlParam);
        JSONObject liveJson = JSONUtil.parseObj(liveResult);
        JSONObject liveDataJson = liveJson.getJSONObject("data");
        String liveRoomId = liveDataJson.getStr("room_id");//房间id
        String liveUid = liveDataJson.getStr("uid");//用户id
        String liveStatus = liveDataJson.getStr("live_status");//直播状态 1已开播 0未开播(下播)
        String liveStartTime = liveDataJson.getStr("live_time");//开播时间
        //0待开播 1待下播
        String status = "0";
        //开播发送提醒
        if ("0".equals(status) && "1".equals(liveStatus)){
            //获取直播间信息
            String liveInfoResult = HttpUtil.get("https://live.bilibili.com/" + roomId);
            //使用正则表达式获取直播间信息
            List<String> liveInfoList = ReUtil.findAllGroup1("<script>window.__NEPTUNE_IS_MY_WAIFU__=(.*?)</script>", liveInfoResult);
            if (liveInfoList.size()>0){
                JSONObject obj = JSONUtil.parseObj(liveInfoList.get(0));
                //直播标题
                String liveTitle = obj.getJSONObject("roomInfoRes").getJSONObject("data").getJSONObject("room_info").getStr("title");
                //用户名称-房间号 liveUname-roomId
                String liveUname = obj.getJSONObject("roomInfoRes").getJSONObject("data").getJSONObject("anchor_info").getJSONObject("base_info").getStr("uname");
                //开播时间
                DateTime livePlayDate = DateUtil.date(Integer.parseInt(liveStartTime));
                System.out.println("标题："+liveTitle);
                System.out.println("用户名："+liveUname+"-"+roomId);
                System.out.println("开播时间："+livePlayDate);
            }
            status = "1";
        }
        //下播发送提醒
        if ("1".equals(status) && "0".equals(liveStatus)){
            status = "0";
        }
        System.out.println(status);
    }
}
